/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.admin.api.entity.SysDictItem;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.i18n.util.MessageUtils;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.system.entity.SysLanguage;
import com.pig4cloud.pigx.system.service.SysLanguageService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统语言
 *
 * @author pigx code generator
 * @date 2022-08-20 10:14:31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/syslanguage" )
@Api(value = "syslanguage", tags = "系统语言管理")
public class SysLanguageController {

    private final  SysLanguageService sysLanguageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysLanguage 系统语言
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('system_syslanguage_view')" )
    public R getSysLanguagePage(Page page, SysLanguage sysLanguage) {
        return R.ok(sysLanguageService.page(page, Wrappers.query(sysLanguage)));
    }


    /**
     * 通过id查询系统语言
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('system_syslanguage_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(sysLanguageService.getById(id));
    }

    /**
     * 新增系统语言
     * @param sysLanguage 系统语言
     * @return R
     */
    @ApiOperation(value = "新增系统语言", notes = "新增系统语言")
    @SysLog("新增系统语言" )
	@PostMapping
    @PreAuthorize("@pms.hasPermission('system_syslanguage_add')" )
    public R save(@RequestBody SysLanguage sysLanguage) {
        return R.ok(sysLanguageService.save(sysLanguage));
    }

    /**
     * 修改系统语言
     * @param sysLanguage 系统语言
     * @return R
     */
    @ApiOperation(value = "修改系统语言", notes = "修改系统语言")
    @SysLog("修改系统语言" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('system_syslanguage_edit')" )
    public R updateById(@RequestBody SysLanguage sysLanguage) {
        return R.ok(sysLanguageService.updateById(sysLanguage));
    }

    /**
     * 通过id删除系统语言
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除系统语言", notes = "通过id删除系统语言")
    @SysLog("通过id删除系统语言" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('system_syslanguage_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(sysLanguageService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param sysLanguage 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('system_syslanguage_export')" )
    public List<SysLanguage> export(SysLanguage sysLanguage) {
        return sysLanguageService.list(Wrappers.query(sysLanguage));
    }

	/**
	 * 查询所有语言
	 * @return
	 */
	@Inner(value = false)
	@ApiOperation(value = "查询所有语言", notes = "查询所有语言")
	@GetMapping("/getSysLanguage" )
	public R getSysLanguage() {
		return R.ok(sysLanguageService.list());
	}


	/**
	 * 系统字典项翻译
	 * @param sysDictItemList 系统字典项集合
	 * @return
	 */
	@Inner(value = false)
	@ApiOperation(value = "系统字典项翻译", notes = "系统字典项翻译")
	@PostMapping("/sysDictItemTranslate")
	public R<List<SysDictItem>> sysDictItemTranslate(@RequestBody List<SysDictItem> sysDictItemList) {
		for (SysDictItem sysDictItem: sysDictItemList) {
			// 根据描述获取多语言配置
			if (!StringUtils.isEmpty(sysDictItem.getRemarks())){
				sysDictItem.setDescription(MessageUtils.get(sysDictItem.getRemarks()));
			}

		}
		return R.ok(sysDictItemList);
	}
}
